@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using BlazorApp.Utils
@using k8s.Models
@inherits BlazorApp.Pages.Common.PageBase

@if (Volume.Projected != null)
{
    <BadgeRibbon Text="@Volume.Name" Placement="start" Color="#832">
        <TextTemplate>
            <span style="color:#DCDCDC">@Index</span>
            <Icon Type="hdd" Theme="outline"/>
            @Volume.Name
        </TextTemplate>
        <ChildContent>
            <Card Bordered>
                <HTable >
                    <HTableColumn Title=@L["Volume"] ExplainField="pod.spec.volumes.projected">
                        <Tag>@Volume.Name</Tag>
                        <ColorfulTag> Projected</ColorfulTag>

                    </HTableColumn>


                    <PropertySimpleView Title=@L["DefaultMode"] ExplainField="pod.spec.volumes.projected.defaultMode" Item="@Volume.Projected.DefaultMode"></PropertySimpleView>

                    @foreach (var p in Volume.Projected.Sources)
                    {
                        @if (p.DownwardAPI is { Items: not null })
                        {
                            @foreach (var item in p.DownwardAPI.Items)
                            {
                                <HTableColumn Title=@L["DownwardAPI"] ExplainField="pod.spec.volumes.projected.sources.downwardAPI.items">
                                    <ColorfulTag Icon="hdd">@item.Path</ColorfulTag>
                                    @if (item.FieldRef != null)
                                    {
                                        <Tag>@item.Mode @item.FieldRef.ApiVersion/@item.FieldRef.FieldPath </Tag>
                                    }
                                    @if (item.ResourceFieldRef != null)
                                    {
                                        <Tag> @item.ResourceFieldRef.ContainerName/@item.ResourceFieldRef.Resource</Tag>
                                    }
                                </HTableColumn>
                            }
                        }


                        @if (p.ConfigMap is { Items: not null })
                        {
                            @foreach (var ktp in p.ConfigMap.Items)
                            {
                                <HTableColumn Title=@L["ConfigMap"] ExplainField="pod.spec.volumes.projected.sources.configMap.items">
                                    <RefView Ref="@TransferToRef(p.ConfigMap)"></RefView>
                                    <ColorfulTag Icon="hdd">@p.ConfigMap.Name</ColorfulTag>
                                    <Tag>@ktp.Key => @ktp.Path</Tag>
                                </HTableColumn>
                            }
                        }

                        @if (p.Secret is { Items: not null })
                        {
                            @foreach (var ktp in p.Secret.Items)
                            {
                                <HTableColumn Title=@L["Secret"] ExplainField="pod.spec.volumes.projected.sources.secret.items">
                                    <RefView Ref="@TransferToRef(p.Secret)"></RefView>
                                    <ColorfulTag Icon="hdd"> @p.Secret.Name</ColorfulTag>
                                    <Tag>@ktp.Key => @ktp.Path</Tag>
                                </HTableColumn>
                            }
                        }

                        @if (p.ServiceAccountToken != null)
                        {
                            <HTableColumn Title=@L["ServiceAccountToken"] ExplainField="pod.spec.volumes.projected.sources.serviceAccountToken">
                                <ColorfulTag Icon="hdd"> @p.ServiceAccountToken.Path</ColorfulTag>
                                <Tag>@p.ServiceAccountToken.Audience @p.ServiceAccountToken.ExpirationSeconds s</Tag>
                            </HTableColumn>
                        }
                    }
                </HTable>
            </Card>
        </ChildContent>
    </BadgeRibbon>
}


@code {

    [Parameter] public V1Volume Volume { get; set; }

    [Parameter] public int Index { get; set; }

    [Parameter] public string Namespace { get; set; }


    private V1ObjectReference TransferToRef(V1ConfigMapProjection item)
    {
        return KubeHelper.GetObjectRef("ConfigMap", Namespace, item.Name);
    }

    private V1ObjectReference TransferToRef(V1SecretProjection item)
    {
        return KubeHelper.GetObjectRef("Secret", Namespace, item.Name);
    }

}
